import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import { HttpExceptionFilter } from './utils/http-exception.filter';
import { WsAdapter } from '@nestjs/platform-ws';
// import { ErrorFilter } from './utils/error.filter';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(
    AppModule,
  );
  // app.enableCors({
  //   origin:(origin,callback)=>{
  //       callback(null,[origin])
  //   },
  //   methods:['GET','POST','OPTIONS','PUT'],
  //   allowedHeaders:[
  //     'DNT',
  //     'Authorization',
  //     'Accept',
  //     'Origin',
  //     'Keep-Alive',
  //     'User-Agent',
  //     'X-Mx-ReqToken',
  //     'X-Data-Type',
  //     'X-Auth-Token',
  //     'X-Requested-With',
  //     'If-Modified-Since',
  //     'Cache-Control',
  //     'Content-Type',
  //     'Range'
  //   ],
  //   exposedHeaders:[
  //     'Content-Length',
  //     'Content-Range',
  //     'Content-Type'
  //   ]
  // });
  app.setGlobalPrefix('api');
  //app.useGlobalGuards(new TokenExtractGuard());
  app.setBaseViewsDir(join(__dirname, '..', 'views'));
  app.setViewEngine('ejs');
  app.useStaticAssets(join(__dirname, 'static'), {
    extensions: ['ejs']
  })
  app.useGlobalFilters(new HttpExceptionFilter())

  //app.useGlobalFilters(new HttpExceptionFilter())
  //app.useGlobalFilters(new ErrorFilter())

  const config = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('doc', app, document);

  app.useWebSocketAdapter(new WsAdapter(app))
  await app.listen(3000);
}
bootstrap();
